<?php

namespace App\Http\Controllers\Home\Hr;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Home\Users as UsersModel;
use App\Models\Home\Frame as FrameModel;
use App\Models\Home\UpSalary;
use Illuminate\Support\Facades\Hash;
use App\User as BaseUsersModel;
use Users;
use Frame;
use Tools;
use Auth;

class PersonController extends Controller
{
    protected $passField = [
        'username','password','duties','describe','rank','salary','frame_id','sex','birthday','native','card_id','graduation_time','education','major','phone','email','wechat'
    ];

    public function person(Request $request)
    {
        $in = [];

        if (!Auth::user()->hasRole('admin')){

            $user = Auth::user()->load('frame');

            $member = Frame::getMember($user->frame->id);

            $member[] = Auth::id();

            $in = ['id',$member];
        }

        $opt = ['type'=>1];
        $uid = 0;
        if($request->isMethod('post')){

            if ($uid = $request->post('uid')){

                $opt = array_merge($opt,['id'=>$uid]);

            }

        }

        $user = Users::getUserList($opt,$in);

        $list = Users::getList($opt,['id','asc'],$in);

        return view('Home.User.Hr.Person.person',compact('user','uid','list'));
    }

    public function person_add(Request $request){
        if($request->isMethod('post')){
            //验证
            $this->validate(request(), [
                'username' => ['required','string',function($attribute, $value, $fail){
                    $user = UsersModel::where('username',$value)->where('status',1)->first();
                    if ($user){
                        $fail(':attribute 已存在。');
                    }
                }],
                'password' => 'required|string|min:8',
                'duties' => 'required|string',
                'leader_id' => [function ($attribute, $value, $fail) {
                    if (request()->post('type') == 1 && !$value) {
                        $fail(':attribute 未选择。');
                    }
                }],
            ],[],[
                'username' => '用户名',
                'password' => '密码',
                'duties' => '职务',
                'leader_id' => '直属领导',
            ]);

            //数据库操作
            $user_data = [];
            $frame_data = [];
            foreach ($request->all() as $key => $val){
                if (!is_numeric($val)){	$val = clean($val);}
                if(in_array($key,$this->passField) && $val){
                    if ($key == 'password'){
                        $val = Hash::make($val);
                    }else{
                        $val = trim($val);
                    }
                    $user_data[$key] = $val;
                }
            }

            $user_data['type'] = 1;
            $user = BaseUsersModel::create($user_data);

            if ($user){
                $leader_id = intval($request->post('leader_id'));
                $leader = FrameModel::find($leader_id);
                if ($leader){
                    $frame_data['uid'] = $user->id;
                    $frame_data['branch_id'] = $leader->branch_id;
                    $frame_data['level'] = $leader->level+1;
                    $frame_data['belong'] = $leader->id;
                    $frame = FrameModel::create($frame_data);
                    $user->frame_id = $frame->id;
                    $user->save();
                    switch ($frame->level){
                        case 1:
                            $user->assignRole('admin');
                            break;
                        case 2:
                            $user->assignRole('leader');
                            break;
                        case 3:
                            $user->assignRole('staff');
                            break;
                    }

                }
            }

            return ['code'=>1];
            //返回结果
        }

        $leader = Frame::getLeader();

        return view('Home.User.Hr.Person.add',compact('leader'));
    }

    public function person_edit(UsersModel $obj){
        if(request() ->isMethod('post')){
            //验证
            $this->validate(request(), [
                'username' => ['nullable','string',function($attribute, $value, $fail) use($obj){
                    if($obj->username != $value){
                        $user = UsersModel::where('username',$value)->where('status',1)->first();
                        if ($user){
                            $fail(':attribute 已存在。');
                        }
                    }
                }],
                'password' => [function ($attribute, $value, $fail) {
                    if ($value && mb_strlen($value)<8) {
                        $fail(':attribute 必须大于 8 个字符。');
                    }
                }],
                'duties' => 'nullable|string',
            ],[],[
                'username' => '用户名',
                'password' => '密码',
                'duties' => '职务',
            ]);

            //数据库操作
            foreach (request()->all() as $key => $val){
                if (!is_numeric($val)){	$val = clean($val);}
                if(in_array($key,$this->passField) && $val){
                    if ($key == 'password'){
                        $val = Hash::make($val);
                    }else{
                        $val = trim($val);
                    }
                    $obj->$key = $val;
                }
            }

            $obj->save();

            //返回结果
            return ['code'=>1];
        }

        return view('Home.User.Hr.Person.edit',compact('obj'));
    }

    public function up_salary(UsersModel $obj){
        $old_rank = $obj->rank;
        $old_salary = $obj->salary;
        if(request() ->isMethod('post')){
            //验证
            $this->validate(request(), [
                'rank' => 'required|string',
                'salary' => 'required|integer'
            ],[],[
                'rank' => '职级',
                'salary' => '薪水'
            ]);

            //数据库操作
            foreach (request()->all() as $key => $val){
                if (!is_numeric($val)){	$val = clean($val);}
                if(in_array($key,$this->passField) && $val){
                    $val = trim($val);
                    $obj->$key = $val;
                }
            }

            if ($old_rank!=$obj->rank || $old_salary!=$obj->salary){
                $obj->save();
                $up_salary_data = [];
                $up_salary_data['assign'] = $obj->id;
                $up_salary_data['before_rank'] = $old_rank;
                $up_salary_data['before_salary'] = $old_salary;
                $up_salary_data['now_rank'] = $obj->rank;
                $up_salary_data['now_salary'] = $obj->salary;
                $obj->up_salary()->create($up_salary_data);
            }

            //返回结果
            return ['code'=>1];
        }

        return view('Home.User.Hr.Person.up_salary',compact('obj'));
    }

    public function promotion(UsersModel $obj,UpSalary $up_obj=null){

        if($up_obj){

            $up_obj->status = -1;

            $res = $up_obj->save();

            $rrturn = [];

            $rrturn['code'] = 0;

            $rrturn['msg'] = '系统错误,请稍后再试';

            if ($res){

                $rrturn['code'] = 1;

                $rrturn['msg'] = 'OK';
            }

            return $rrturn;

        }

        $obj->load(['up_salary'=>function($query){

            $query->where('status',1);

        }]);

        return view('Home.User.Hr.Person.promotion',compact('obj'));

    }

}